package com.zxl.always.on.display.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.base.BaseAppCompatActivity;
import com.zxl.always.on.display.constant.ICustomAction;
import com.zxl.always.on.display.constant.LockerEnv;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.tools.BitmapUtils;
import com.zxl.always.on.display.tools.FileUtil;
import com.zxl.always.on.display.ui.widget.clock.ImageClockView;

/**
 * Created by Administrator on 2018/3/21.
 */

public class ImageSettingActivity extends BaseAppCompatActivity implements View.OnClickListener {

    private LinearLayout mChangeImageView;

    private ImageClockView mImageClockView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_settings);
        findViewById(R.id.settings_custom_action_bar_cancel_button).setOnClickListener(this);
        findViewById(R.id.settings_custom_action_bar_apply_button).setOnClickListener(this);
        mChangeImageView = (LinearLayout) findViewById(R.id.change_image);
        mChangeImageView.setOnClickListener(this);

        mImageClockView = (ImageClockView) findViewById(R.id.clock_image);
        mImageClockView.getImageView().setOnClickListener(this);
        mChangeImageView.setVisibility(FileUtil.isFileExist(LockerEnv.Path.IMAGE_SHOW) ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_theme_clock_image:
                if (FileUtil.isFileExist(LockerEnv.Path.IMAGE_SHOW)) {
                    break;
                }
            case R.id.change_image:
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 100);
                break;
            case R.id.settings_custom_action_bar_apply_button:
                if (!FileUtil.isFileExist(LockerEnv.Path.IMAGE_SHOW) && !FileUtil.isFileExist(LockerEnv.Path.IMAGE_TMP)) {
                    Toast.makeText(this, "请选择您要显示的图片", Toast.LENGTH_SHORT).show();
                    return;
                }
                ImageClockView.savePath();
                sendBroadcast(new Intent(ICustomAction.ACTION_CLOCK_CHANGE).putExtra("data", ClockStyle.CLOCK_IMAGE));
                finish();
                break;
            case R.id.settings_custom_action_bar_cancel_button:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if (resultCode != RESULT_CANCELED) {
            new Thread() {

                @Override
                public void run() {
                    super.run();
                    final Bitmap bitmap = BitmapUtils.createBitmap(ImageSettingActivity.this, data);
                    if (null != bitmap) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mImageClockView.getImageView().setImageBitmap(bitmap);
                                mChangeImageView.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                }
            }.start();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public static void startPage(Activity activity) {
        activity.startActivity(new Intent(activity, ImageSettingActivity.class));
    }
}
